package com.itheima.testlearn.test8;

public class Test {
    public static void main(String[] args){
        Student s1 =new Student(1,"dyx",22);
        Student s2 =new Student(2,"zyh",23);
        //Student s3 =new Student(3,"zgl",26);
        Student[] arr = new Student[3];
        arr[0]=s1;
        arr[1]=s2;
        //arr[2]=s3;
        Student s4 =new Student(9,"sjh",21);

        boolean flag = contains(arr,s4.getId());

        if(flag){
            System.out.println("该学生的学号已存在！");
        }
        else{
            if(arr.length==getCount(arr)){
                Student[] newArr= createNewArr(arr);
                newArr[getCount(arr)]=s4;
            }
            else{
                arr[getCount(arr)]=s4;
            }
        }

        int indexId = getIndex_id(arr,2);
        if(indexId!=-1){
            arr[indexId]=null;
            System.out.println("已删除"+indexId+"号索引");
        }
        else{
            System.out.println("该索引不存在");
        }

        

    }


    public static int getIndex_id(Student[] arr,int id){
        for(int i=0;i<getCount(arr);i++){
            if(arr[i].getId()==id){
                return i;
            }
        }

        return -1;
    }

    public static Student[] createNewArr(Student[] arr){
        Student[] tmp = new Student[arr.length+1];
        int i=0;
        for(Student stu:arr){
            tmp[i++]=stu;
        }
        return tmp;
    }

    public static int getCount(Student[] arr){
        int sum=0;
        for(Student student:arr){
            sum++;
        }
        return sum;
    }

    public static boolean contains(Student[] arr,int id){
        for (Student student : arr) {
            if(student!=null) {
                if (student.getId() == id) return true;
            }
        }
        return false;
    }

}
